package com.pokermind.model.enums;

import org.springframework.http.HttpStatus;

/**
 * 错误码枚举 - 标准数字编码格式
 * 编码规则：
 * 1000-1999: 手牌管理错误
 * 2000-2999: 玩家和座位错误  
 * 3000-3999: 行动验证错误
 * 4000-4999: AI服务错误
 * 5000-5999: 认证和权限错误
 * 6000-6999: 用户错误
 * 7000-7999: 数据验证错误
 * 8000-8999: 通用错误
 * 
 * @author PokerMind Team
 */
public enum ErrorCode {
    
    // 手牌管理错误 (1000-1999)
    HAND_NOT_FOUND(HttpStatus.NOT_FOUND, "1001", "hand not found"),
    HAND_ALREADY_EXISTS(HttpStatus.CONFLICT, "1002", "hand already exists"),
    HAND_ALREADY_COMPLETED(HttpStatus.CONFLICT, "1003", "hand already completed"),
    HAND_INVALID_STATE(HttpStatus.UNPROCESSABLE_ENTITY, "1004", "invalid hand state"),
    HAND_INVALID_STREET_TRANSITION(HttpStatus.UNPROCESSABLE_ENTITY, "1005", "invalid street transition"),
    HAND_BOARD_CARDS_MISMATCH(HttpStatus.UNPROCESSABLE_ENTITY, "1006", "board cards mismatch"),
    
    // 玩家和座位错误 (2000-2999)
    PLAYER_SEAT_OCCUPIED(HttpStatus.CONFLICT, "2001", "seat occupied"),
    PLAYER_NOT_FOUND(HttpStatus.NOT_FOUND, "2002", "player not found"),
    PLAYER_NO_HERO(HttpStatus.UNPROCESSABLE_ENTITY, "2003", "no hero player"),
    PLAYER_MULTIPLE_HERO(HttpStatus.UNPROCESSABLE_ENTITY, "2004", "multiple hero players"),
    PLAYER_INSUFFICIENT_PLAYERS(HttpStatus.UNPROCESSABLE_ENTITY, "2005", "insufficient players"),
    PLAYER_INVALID_STACK(HttpStatus.UNPROCESSABLE_ENTITY, "2006", "invalid stack amount"),
    
    // 行动验证错误 (3000-3999)
    ACTION_INVALID_SEAT(HttpStatus.UNPROCESSABLE_ENTITY, "3001", "invalid seat"),
    ACTION_WRONG_TURN(HttpStatus.UNPROCESSABLE_ENTITY, "3002", "wrong turn"),
    ACTION_INVALID_AMOUNT(HttpStatus.UNPROCESSABLE_ENTITY, "3003", "invalid action amount"),
    ACTION_INSUFFICIENT_STACK(HttpStatus.UNPROCESSABLE_ENTITY, "3004", "insufficient stack"),
    ACTION_INVALID_TYPE(HttpStatus.UNPROCESSABLE_ENTITY, "3005", "invalid action type"),
    ACTION_MIN_RAISE_VIOLATION(HttpStatus.UNPROCESSABLE_ENTITY, "3006", "min raise violation"),
    ACTION_PLAYER_NOT_ACTIVE(HttpStatus.UNPROCESSABLE_ENTITY, "3007", "player not active"),
    
    // AI服务错误 (4000-4999)
    AI_SERVICE_UNAVAILABLE(HttpStatus.SERVICE_UNAVAILABLE, "4001", "ai service unavailable"),
    AI_ANALYSIS_TIMEOUT(HttpStatus.GATEWAY_TIMEOUT, "4002", "ai analysis timeout"),
    AI_INSUFFICIENT_DATA(HttpStatus.UNPROCESSABLE_ENTITY, "4003", "insufficient data for ai"),
    AI_INVALID_PARAMETERS(HttpStatus.UNPROCESSABLE_ENTITY, "4004", "invalid ai parameters"),
    AI_QUOTA_EXCEEDED(HttpStatus.TOO_MANY_REQUESTS, "4005", "ai quota exceeded"),
    AI_HAND_NOT_ACTIVE(HttpStatus.UNPROCESSABLE_ENTITY, "4006", "hand not active for ai"),
    AI_NOT_HERO_TURN(HttpStatus.FORBIDDEN, "4007", "not hero turn"),
    AI_NO_HERO_IN_HAND(HttpStatus.FORBIDDEN, "4008", "no hero in hand"),
    AI_HERO_ID_MISSING(HttpStatus.BAD_REQUEST, "4009", "hero id missing"),
    AI_HERO_ID_MISMATCH(HttpStatus.FORBIDDEN, "4010", "hero id mismatch"),
    AI_INVALID_REQUEST(HttpStatus.BAD_REQUEST, "4011", "invalid ai request"),
    
    // 认证和权限错误 (5000-5999)
    AUTH_TOKEN_INVALID(HttpStatus.UNAUTHORIZED, "5001", "invalid token"),
    AUTH_TOKEN_EXPIRED(HttpStatus.UNAUTHORIZED, "5002", "token expired"),
    AUTH_INSUFFICIENT_SCOPE(HttpStatus.FORBIDDEN, "5003", "insufficient scope"),
    AUTH_RATE_LIMITED(HttpStatus.TOO_MANY_REQUESTS, "5004", "rate limited"),
    
    // 用户错误 (6000-6999)
    USER_NOT_FOUND(HttpStatus.NOT_FOUND, "6001", "user not found"),
    USER_ALREADY_EXISTS(HttpStatus.CONFLICT, "6002", "user already exists"),
    
    // 数据验证错误 (7000-7999)
    VALIDATION_EMPTY_FIELD(HttpStatus.BAD_REQUEST, "7001", "required field missing"),
    VALIDATION_FIELD_TOO_LONG(HttpStatus.BAD_REQUEST, "7002", "field too long"),
    VALIDATION_INVALID_SEAT(HttpStatus.BAD_REQUEST, "7003", "invalid seat number"),
    VALIDATION_DUPLICATE_FIELD(HttpStatus.BAD_REQUEST, "7004", "duplicate field value"),
    VALIDATION_INVALID_CARDS(HttpStatus.BAD_REQUEST, "7005", "invalid card format"),
    VALIDATION_TOO_MANY_PLAYERS(HttpStatus.BAD_REQUEST, "7006", "too many players"),

    // 通用错误 (8000-8999)
    VALIDATION_FAILED(HttpStatus.BAD_REQUEST, "8001", "validation failed"),
    PARAMETER_BINDING_FAILED(HttpStatus.BAD_REQUEST, "8002", "parameter binding failed"),
    PARAMETER_TYPE_MISMATCH(HttpStatus.BAD_REQUEST, "8003", "parameter type mismatch"),
    JSON_PARSE_ERROR(HttpStatus.BAD_REQUEST, "8004", "json parse error"),
    INVALID_ARGUMENT(HttpStatus.BAD_REQUEST, "8005", "invalid argument"),
    INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "8500", "internal server error"),
    UNKNOWN_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "8999", "unknown error");
    
    private final HttpStatus httpStatus;
    private final String code;
    private final String message;
    
    ErrorCode(HttpStatus httpStatus, String code, String message) {
        this.httpStatus = httpStatus;
        this.code = code;
        this.message = message;
    }
    
    public HttpStatus getHttpStatus() {
        return httpStatus;
    }
    
    public String getCode() {
        return code;
    }
    
    public String getMessage() {
        return message;
    }
    
    /**
     * 根据错误码字符串查找枚举
     */
    public static ErrorCode fromCode(String code) {
        for (ErrorCode errorCode : values()) {
            if (errorCode.getCode().equals(code)) {
                return errorCode;
            }
        }
        return UNKNOWN_ERROR;
    }
}